package net.kindroid.fm;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * channel View
 * 
 * @author huasong.chai
 */
public class DigitImageView extends LinearLayout
{
	// digit
	private ImageView mImage_high_digit;
	private ImageView mImage_decade_digit;
	private ImageView mImage_bit_digit;
	private ImageView mImage_dot_digit;
	private ImageView mImage_decimal_digit;
	private ImageView mImage_mhz_icon;

	// reflect digit
	private ImageView mImage_high_digit_reflect;
	private ImageView mImage_decade_digit_reflect;
	private ImageView mImage_bit_digit_reflect;
	private ImageView mImage_dot_digit_reflect;
	private ImageView mImage_decimal_digit_reflect;
	private ImageView mImage_mhz_icon_reflect;

	private int mChannel = 875;

	public int getChannel()
	{
		return mChannel;
	}

	public void setChannel(int channel)
	{
		mChannel = channel;
		updateBackgroundResource();
	}

	public DigitImageView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		LinearLayout digitlayout = new LinearLayout(context);
		LinearLayout reflectDigitLayout = new LinearLayout(context);

		digitlayout.setOrientation(LinearLayout.HORIZONTAL);
		reflectDigitLayout.setOrientation(LinearLayout.HORIZONTAL);

		LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params.gravity = Gravity.BOTTOM;
		params.leftMargin = 0;
		digitlayout.setLayoutParams(params);
		reflectDigitLayout.setLayoutParams(params);

		mImage_high_digit = new ImageView(context, attrs);
		mImage_decade_digit = new ImageView(context, attrs);
		mImage_bit_digit = new ImageView(context, attrs);
		mImage_dot_digit = new ImageView(context, attrs);
		mImage_decimal_digit = new ImageView(context, attrs);
		mImage_mhz_icon = new ImageView(context, attrs);

		mImage_high_digit_reflect = new ImageView(context, attrs);
		mImage_decade_digit_reflect = new ImageView(context, attrs);
		mImage_bit_digit_reflect = new ImageView(context, attrs);
		mImage_dot_digit_reflect = new ImageView(context, attrs);
		mImage_decimal_digit_reflect = new ImageView(context, attrs);
		mImage_mhz_icon_reflect = new ImageView(context, attrs);

		mImage_high_digit.setBackgroundResource(setDigitResource(0));
		mImage_decade_digit.setBackgroundResource(setDigitResource(0));
		mImage_bit_digit.setBackgroundResource(setDigitResource(0));
		mImage_dot_digit.setBackgroundResource(setDigitResource(-1));
		mImage_decimal_digit.setBackgroundResource(setDigitResource(0));

		mImage_mhz_icon.setBackgroundResource(setDigitResource(-2));

		mImage_high_digit_reflect.setBackgroundResource(setReflectDigitResource(0));
		mImage_decade_digit_reflect.setBackgroundResource(setReflectDigitResource(0));
		mImage_bit_digit_reflect.setBackgroundResource(setReflectDigitResource(0));
		mImage_dot_digit_reflect.setBackgroundResource(setReflectDigitResource(-1));
		mImage_decimal_digit_reflect.setBackgroundResource(setReflectDigitResource(0));
		mImage_mhz_icon_reflect.setBackgroundResource(setReflectDigitResource(-2));

		LinearLayout.LayoutParams decade_digit_Param = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		decade_digit_Param.leftMargin = 3;
		decade_digit_Param.rightMargin = 2;
		decade_digit_Param.gravity = Gravity.CENTER_VERTICAL;
		mImage_decade_digit.setLayoutParams(decade_digit_Param);

		LinearLayout.LayoutParams dot_digit_Param = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		dot_digit_Param.leftMargin = 0;
		dot_digit_Param.rightMargin = 1;
		dot_digit_Param.gravity = Gravity.CENTER_VERTICAL;
		mImage_dot_digit.setLayoutParams(dot_digit_Param);

		LinearLayout.LayoutParams decimal_digit_Param = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		decimal_digit_Param.leftMargin = 0;
		decimal_digit_Param.rightMargin = 16;
		decimal_digit_Param.gravity = Gravity.CENTER_VERTICAL;
		mImage_decimal_digit.setLayoutParams(decimal_digit_Param);

		LinearLayout.LayoutParams mhzParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		mhzParam.leftMargin = 0;
		mhzParam.gravity = Gravity.CENTER_VERTICAL;
		mImage_mhz_icon.setLayoutParams(mhzParam);

		digitlayout.addView(mImage_high_digit);
		digitlayout.addView(mImage_decade_digit);
		digitlayout.addView(mImage_bit_digit);
		digitlayout.addView(mImage_dot_digit);
		digitlayout.addView(mImage_decimal_digit);
		digitlayout.addView(mImage_mhz_icon);

		decade_digit_Param = null;
		decade_digit_Param = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		decade_digit_Param.leftMargin = 3;
		decade_digit_Param.rightMargin = 2;
		decade_digit_Param.gravity = Gravity.CENTER_VERTICAL;
		mImage_decade_digit_reflect.setLayoutParams(decade_digit_Param);
		dot_digit_Param = null;
		dot_digit_Param = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		dot_digit_Param.leftMargin = 0;
		dot_digit_Param.rightMargin = 1;
		dot_digit_Param.gravity = Gravity.CENTER_VERTICAL;
		mImage_dot_digit_reflect.setLayoutParams(dot_digit_Param);
		mImage_decimal_digit_reflect.setLayoutParams(decimal_digit_Param);

		mhzParam = null;
		mhzParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		mhzParam.leftMargin = 0;
		mImage_mhz_icon_reflect.setLayoutParams(mhzParam);

		mImage_mhz_icon.setLayoutParams(mhzParam);
		reflectDigitLayout.addView(mImage_high_digit_reflect);
		reflectDigitLayout.addView(mImage_decade_digit_reflect);
		reflectDigitLayout.addView(mImage_bit_digit_reflect);
		reflectDigitLayout.addView(mImage_dot_digit_reflect);
		reflectDigitLayout.addView(mImage_decimal_digit_reflect);
		reflectDigitLayout.addView(mImage_mhz_icon_reflect);

		setClickable(false);
		setFocusable(true);
		setOrientation(LinearLayout.VERTICAL);
		addView(digitlayout);
		addView(reflectDigitLayout);
	}

	/**
	 * update digit bitmap
	 */
	private void updateBackgroundResource()
	{
		int high_digit = 0;
		int decade_digit = 0;
		int bit_digit = 0;
		int decimal_digit = 0;
		if (mChannel >= 1000)
		{
			high_digit = 1;
		}
		decade_digit = (mChannel - high_digit * 1000) / 100;
		bit_digit = (mChannel - high_digit * 1000 - decade_digit * 100) / 10;
		decimal_digit = mChannel - high_digit * 1000 - decade_digit * 100 - bit_digit * 10;
		if (high_digit == 0)
		{
			mImage_high_digit.setBackgroundResource(setDigitResource(10));
			mImage_high_digit_reflect.setBackgroundResource(setReflectDigitResource(10));
		} else
		{
			mImage_high_digit.setBackgroundResource(setDigitResource(high_digit));
			mImage_high_digit_reflect.setBackgroundResource(setReflectDigitResource(high_digit));
		}
		mImage_decade_digit.setBackgroundResource(setDigitResource(decade_digit));
		mImage_bit_digit.setBackgroundResource(setDigitResource(bit_digit));
		mImage_dot_digit.setBackgroundResource(setDigitResource(-1));
		mImage_decimal_digit.setBackgroundResource(setDigitResource(decimal_digit));

		mImage_decade_digit_reflect.setBackgroundResource(setReflectDigitResource(decade_digit));
		mImage_bit_digit_reflect.setBackgroundResource(setReflectDigitResource(bit_digit));
		mImage_dot_digit_reflect.setBackgroundResource(setReflectDigitResource(-1));
		mImage_decimal_digit_reflect.setBackgroundResource(setReflectDigitResource(decimal_digit));
	}

	/**
	 * digit resource
	 * 
	 * @param digit
	 * @return
	 */
	private int setDigitResource(int digit)
	{
		int resource = R.drawable.zero;
		switch (digit)
		{
			case -2:
				resource = R.drawable.mhz;
				break;
			case -1:
				resource = R.drawable.dot;
				break;
			case 0:
				resource = R.drawable.zero;
				break;
			case 1:
				resource = R.drawable.one;
				break;
			case 2:
				resource = R.drawable.two;
				break;
			case 3:
				resource = R.drawable.three;
				break;
			case 4:
				resource = R.drawable.four;
				break;
			case 5:
				resource = R.drawable.five;
				break;
			case 6:
				resource = R.drawable.six;
				break;
			case 7:
				resource = R.drawable.seven;
				break;
			case 8:
				resource = R.drawable.eight;
				break;
			case 9:
				resource = R.drawable.nine;
				break;
			case 10:
				resource = R.drawable.digit_transparent;
				break;
		}
		return resource;
	}

	/**
	 * reflect digit
	 * 
	 * @param digit
	 * @return
	 */
	private int setReflectDigitResource(int digit)
	{
		int resource = R.drawable.zero_;
		switch (digit)
		{
			case -2:
				resource = R.drawable.mhz_;
				break;
			case -1:
				resource = R.drawable.dot_;
				break;
			case 0:
				resource = R.drawable.zero_;
				break;
			case 1:
				resource = R.drawable.one_;
				break;
			case 2:
				resource = R.drawable.two_;
				break;
			case 3:
				resource = R.drawable.three_;
				break;
			case 4:
				resource = R.drawable.four_;
				break;
			case 5:
				resource = R.drawable.five_;
				break;
			case 6:
				resource = R.drawable.six_;
				break;
			case 7:
				resource = R.drawable.seven_;
				break;
			case 8:
				resource = R.drawable.eight_;
				break;
			case 9:
				resource = R.drawable.nine_;
				break;
			case 10:
				resource = R.drawable.digit_transparent;
				break;
		}
		return resource;
	}

}
